package com.example.celldian.db;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBInventory extends SQLiteOpenHelper {

	public DBInventory(Context activity) {
		super(activity, "Celldian.db", null, 1);
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		
		String query = "CREATE TABLE inventories ( charname TEXT PRIMARY KEY, itemname TEXT, " +
					   "itemlevel TEXT, type TEXT, description TEXT)";
		
		db.execSQL(query);	
		
		Log.i("DATABASE DBInventory:","TABLE inventories SUCCESSFULY CREATED!");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		
		String query = "DROP TABLE IF EXISTS inventories";
		
		db.execSQL(query);
		onCreate(db);				
		
		Log.i("DATABASE DBInventory:","UPDATE SUCCESSFUL!");
	}
	
	public void insertInventoryValue(HashMap<String ,String> inventoryValues) {
			
		SQLiteDatabase db = this.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		
		values.put("charname", inventoryValues.get("charname"));
		values.put("itemname", inventoryValues.get("itemname"));
		values.put("itemlevel", inventoryValues.get("itemlevel"));
		values.put("type", inventoryValues.get("type"));
		values.put("description", inventoryValues.get("description"));
		
		db.insert("inventories", null, values);
		db.close();
		
		Log.i("DATABASE DBInventory:","INVENTORY VALUE INSERT WAS SUCCESSFUL!");
	}
	
	public int updateInventoryValue(HashMap<String ,String> inventoryValues) {
		
		SQLiteDatabase db = this.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		
		values.put("charname", inventoryValues.get("charname"));
		values.put("itemname", inventoryValues.get("itemname"));
		values.put("itemlevel", inventoryValues.get("itemlevel"));
		values.put("type", inventoryValues.get("type"));
		values.put("description", inventoryValues.get("description"));
		
		Log.i("DATABASE DBInventory:","INVENTORY VALUE UPDATE WAS SUCCESSFUL!");
		
		return db.update("inventories", values, "charname" + "?=", new String[] {inventoryValues.get("charname")});
	}
	
	public void deleteInventoryValue(String charname) {
		
		SQLiteDatabase db = this.getWritableDatabase();
		
		String query = "DELETE FROM inventories WHERE charname='" + charname + "'";
		
		db.execSQL(query);
		
		Log.i("DATABASE DBInventory:","INVENTORY VALUE DELETE WAS SUCCESSFUL!");
	}
	
	public ArrayList<HashMap<String, String>> getInventories() {
		
		ArrayList<HashMap<String, String>> inventoriesList = new ArrayList<HashMap<String, String>>();
		SQLiteDatabase db = this.getReadableDatabase();
		
		String query = "SELECT * FROM inventories";
		
		Cursor cursor = db.rawQuery(query, null);
		
		if (cursor.moveToFirst()) {
			
			do {
				HashMap<String, String> inventory = new HashMap<String, String>();
				
				inventory.put("charname", cursor.getColumnName(0));
				inventory.put("itemname", cursor.getColumnName(1));
				inventory.put("itemlevel", cursor.getColumnName(2));
				inventory.put("type", cursor.getColumnName(3));
				inventory.put("description", cursor.getColumnName(4));
				
				inventoriesList.add(inventory);
			} while(cursor.moveToNext());
		}
		
		Log.i("DATABASE DBInventory:","INVENTORIES RETURN WAS SUCCESSFUL!");
		
		return inventoriesList;
	}
	
	public ArrayList<String> getInventory(String charname){
		
		ArrayList<String> inventories = new ArrayList<String>();
		SQLiteDatabase db = this.getReadableDatabase();
		
		String query = "SELECT * FROM inventories WHERE charname='" + charname + "'";
		
		Cursor cursor = db.rawQuery(query, null);
		
		if (cursor.moveToFirst()) {
			
			do {
				inventories.add(cursor.getColumnName(1));
				inventories.add(cursor.getColumnName(2));
				inventories.add(cursor.getColumnName(3));
				inventories.add(cursor.getColumnName(4));
			} while(cursor.moveToNext());
		}
		
		Log.i("DATABASE DBInventory:","INVENTORY RETURN WAS SUCCESSFUL!");
		
		return inventories;
	}
}
